package com.example.mansystem.controller;


import com.example.mansystem.Annotation.RequiredApi;
import com.example.mansystem.dto.AddRoleDto;
import com.example.mansystem.dto.ChangePermissionDto;
import com.example.mansystem.dto.DeleteRoleDto;
import com.example.mansystem.entity.CallResultMsg;
import com.example.mansystem.entity.RoleDO;
import com.example.mansystem.exception.UniformReponseHandler;
import com.example.mansystem.service.RoleService;
import com.example.mansystem.vo.RoleInfo;
import com.example.mansystem.vo.RouteApiList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @GetMapping("/permissionlist")
    public CallResultMsg GetRolePermissionList()
    {
        List<RoleDO> allDePermission = roleService.getRoleList();
        ArrayList<RoleInfo> res = new ArrayList<RoleInfo>();
        for(int i=0;i<allDePermission.size();i++)
        {
            res.add(RoleInfo.builder().
                        roleId(allDePermission.get(i).getRoleId()).
                        roleName(allDePermission.get(i).getRoleName()).
                        route(roleService.getRouteByRoleId(allDePermission.get(i).getRoleId())).
                        api(roleService.getApiByRoleId(allDePermission.get(i).getRoleId())).
                        build());
        }
        return new UniformReponseHandler<ArrayList<RoleInfo>>().sendSuccessResponse(res);
    }

    @GetMapping("/allrole")
    public CallResultMsg GetAllRole()
    {
        return new UniformReponseHandler<List<RoleDO>>().sendSuccessResponse(roleService.getAllRole());
    }

    @GetMapping("/routeApiList")
    public CallResultMsg GetRouteAndApiList()
    {
        return new UniformReponseHandler<RouteApiList>().sendSuccessResponse(roleService.getRouteApiList());
    }

    @RequiredApi("role:edit")
    @PostMapping("/changePermission")
    public CallResultMsg ChangePermission(HttpServletRequest request, @RequestBody @Validated ChangePermissionDto validator)
    {
        roleService.ChangePermission(validator);
        return new UniformReponseHandler<String>().sendSuccessResponse("SUCCESS");
    }
    @RequiredApi("role:add")
    @PostMapping("/addRole")
    public CallResultMsg AddRole(HttpServletRequest request, @RequestBody @Validated AddRoleDto validator)
    {
        roleService.addRole(validator.getRolename());
        return new UniformReponseHandler<String>().sendSuccessResponse("SUCCESS");
    }

    @RequiredApi("role:delete")
    @PostMapping("/deleteRole")
    public CallResultMsg DeleteRole(HttpServletRequest request, @RequestBody DeleteRoleDto validator)
    {
        roleService.deleteRole(validator.getRoleId());
        return new UniformReponseHandler<String>().sendSuccessResponse("SUCCESS");
    }
}
